package com.weed.helper;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
/*
import net.sf.jazzlib.CRC32;
import net.sf.jazzlib.CheckedInputStream;
import net.sf.jazzlib.CheckedOutputStream;
import net.sf.jazzlib.GZIPInputStream;
import net.sf.jazzlib.GZIPOutputStream;
import net.sf.jazzlib.ZipEntry;
import net.sf.jazzlib.ZipOutputStream;
*/
import java.util.zip.*;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Administrator
 */
public class ZipUtil {

    public static OutputStream gzipOutputStream(Object object) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
  //      CheckedOutputStream cos = new CheckedOutputStream(baos,
  //              new CRC32());
        GZIPOutputStream zipout = new GZIPOutputStream(baos,128);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(zipout);
        objectOutputStream.writeObject(object);
        objectOutputStream.flush();
        objectOutputStream.close();
        zipout.flush();
        zipout.close();
        return baos;
    }
    
    public static void gzipOutputStream(Object object,OutputStream os) throws IOException {
        CheckedOutputStream cos = new CheckedOutputStream(os,
                new CRC32());
        GZIPOutputStream zipout = new GZIPOutputStream(cos);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(zipout);
        objectOutputStream.writeObject(object);
        objectOutputStream.flush();
        objectOutputStream.close();
        zipout.flush();
        zipout.close();
    }
    
    public static OutputStream zipOutputStream(Object object) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        CheckedOutputStream cos = new CheckedOutputStream(baos,
                new CRC32());
        ZipOutputStream zipout = new ZipOutputStream(cos);
        ZipEntry zipEntry = new ZipEntry("data.obj");
        zipout.putNextEntry(zipEntry);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(zipout);
        objectOutputStream.writeObject(object);
        objectOutputStream.flush();
        objectOutputStream.close();
        zipout.flush();
        zipout.close();
        return baos;
    }

    public static Object gzipInputStream(InputStream is) throws IOException, ClassNotFoundException {
        CheckedInputStream cis =  new CheckedInputStream(is,
                new CRC32());
        long time = System.currentTimeMillis();
        GZIPInputStream zipin = new GZIPInputStream(cis,1024*1024);
        ObjectInputStream objectInputStream = new ObjectInputStream(zipin);
        Object obj = objectInputStream.readObject();
        zipin.close();
        return obj;
    }
    /*
    public static OutputStream Z7OutputStream(Object object) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayOutputStream bos = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            bos = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(bos);
            objectOutputStream.writeObject(object);
            objectOutputStream.flush();
            objectOutputStream.close();
            
            SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
            encoder.SetAlgorithm(1);
            encoder.SetDictionarySize(new Integer(1 << 10));
            encoder.SetNumFastBytes(8);
            encoder.SetMatchFinder(2);
            encoder.SetLcLpPb(3, 0, 2);
            encoder.SetEndMarkerMode(true);
            encoder.WriteCoderProperties(baos);
            //-----------------------------------------------------------
            ByteArrayInputStream byteInputStrem = new ByteArrayInputStream(bos.toByteArray());
            int fileSize = bos.size();
            for (int i = 0; i < 8; i++) {
                baos.write((int) (fileSize >>> (8 * i)) & 0xFF);
            }
            encoder.Code(byteInputStrem, baos, -1, -1, null);//����
            return baos;
        } catch (IOException e) {
            throw new Error(e);
        } finally {
            bos = null;
            objectOutputStream = null;
        }
    }
*/
    public static void toFile(OutputStream os, String filename) throws FileNotFoundException, IOException {
        FileOutputStream outStream = new FileOutputStream(filename);
        outStream.write(((ByteArrayOutputStream) os).toByteArray());
        outStream.flush();
        outStream.close();
    }

    public static void main(String[] args) {
        try {
            ArrayList list = new ArrayList();
            for (int i = 0; i < 300000; i++) {
                try {
                    list.add(new Object[]{"Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9", "Column10"});
                } catch (Exception ex) {
                }
            }
           long time = System.currentTimeMillis();
   /**
            OutputStream os = null;
            System.out.println("7Zѹ���ʱ��" + (System.currentTimeMillis()-time)+"����");
            ZipUtil.toFile(os, "output/out.7z");
            long time1 = System.currentTimeMillis();
    */
            OutputStream os1 = ZipUtil.gzipOutputStream(list);
            System.out.println("gzip" + (System.currentTimeMillis()-time)+"毫秒");
            ZipUtil.toFile(os1, "gzipout.zip");
            long time2 = System.currentTimeMillis();
            OutputStream os2 = ZipUtil.zipOutputStream(list);
            System.out.println("ZIP" + (System.currentTimeMillis()-time2)+"毫秒");
            ZipUtil.toFile(os2, "zipout.zip");
        } catch (Exception e) {
            e.printStackTrace();
            ;
        }
    }
}
